package com.test.burnincash.activity_fragment_burnin;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.test.burnincash.bean_burnin.config_info_burnin.AppShowInfo_burnin;
import com.test.burnincash.constant_burnin.Constant_burnin;
import com.test.burnincash.util_burnin.PermissionUtil_burnin;
import com.test.burnincash.util_burnin.StringUtil_burnin;
import com.test.burnincash.util_burnin.helper_burnin.UserInfoHelper_burnin;
import com.test.burnincash.util_burnin.http_burnin.HttpManager_burnin;
import com.test.burnincash.R;
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.loan_burnin.LoanMainActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.login_burnin.LoginPhoneNumActivity_burnin;


public class ApplyPermissionActivity_burnin extends BaseActivity_burnin {

    //private UserAgreementDialog userAgreementDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_permission_burnin);
        init();
    }

    @SuppressLint("SetTextI18n")
    private void init(){

        TextView agreementTv = findViewById(R.id.activity_apply_permission_tv_agreement);
        agreementTv.setText(getString(R.string.activity_apply_permission_agreement_1));
        SpannableString spannableStr = new SpannableString(getString(R.string.activity_apply_permission_agreement_2));
        spannableStr.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                Uri uri = Uri.parse(StringUtil_burnin.getSafeString(Constant_burnin.Privacy_Nigeria));
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(ContextCompat.getColor(ApplyPermissionActivity_burnin.this,R.color.yellow_burnin));
                ds.setFlags(Paint.UNDERLINE_TEXT_FLAG);
            }
        },0,spannableStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        agreementTv.append(spannableStr);
        agreementTv.append(getString(R.string.activity_apply_permission_agreement_3));
        agreementTv.setMovementMethod(LinkMovementMethod.getInstance());

        findViewById(R.id.activity_apply_permission_btn_next).setOnClickListener(view -> {
            if (!PermissionUtil_burnin.checkLaunchPermissions(this, AppShowInfo_burnin.getInstance().getPermissions())){
                requestLaunchPermissions_burnin(AppShowInfo_burnin.getInstance().getPermissions());
            }
        });
    }

    @Override
    public void onRationaleAccepted(int requestCode) {
        if (!PermissionUtil_burnin.checkLaunchPermissions(this, AppShowInfo_burnin.getInstance().getPermissions()))
            requestLaunchPermissions_burnin(AppShowInfo_burnin.getInstance().getPermissions());
    }


    @Override
    public void onRationaleDenied(int requestCode) {
        finish();
    }

    @Override
    protected void onLaunchPermissionsAllPass_burnin() {
        try {
            checkUserProcess();
        } catch (Exception e) {
            e.printStackTrace();
            checkUserProcess();
        }
    }

    //权限通过后执行下一步流程
    private void checkUserProcess() {
        //步骤4 检查是否已登录
        if (UserInfoHelper_burnin.getInstance().didLogin()) {
            //步骤5 查询用户状态
            HttpManager_burnin.getInstance().requestUserProcess();
        } else {
            //步骤6 未登录，去登录
            startActivityForSingleTop_burnin(LoginPhoneNumActivity_burnin.class);
        }
    }

    @Override
    public void onUserProcessResponse_burnin() {
        startActivityForSingleTop_burnin(LoanMainActivity_burnin.class);
    }

}